package com.dk.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dk.entity.TContent;
import com.dk.entity.TContentOrder;
import com.dk.mapper.TContentOrderMapper;
import com.dk.service.ITContentService;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.List;

@RocketMQMessageListener(consumerGroup = "order-group", topic = "orderid")
@Component
public class OrderMq implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {
    @Autowired
    private TContentOrderMapper tContentOrderMapper;
    @Autowired
    private ITContentService itContentService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(String s) {

    }

    @Override
    public void prepareStart(DefaultMQPushConsumer defaultMQPushConsumer) {
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                //将获取到的消息转为对象
                MessageExt messageExt = list.get(0);
                byte[] body = messageExt.getBody();
                String tContent = new String(body);
                TContent tContent1 = JSONObject.parseObject(tContent, TContent.class);

                //将订单号放入redis用来判断重试次数
                String orderId = tContent1.getOrderId();
//                redisTemplate.setValueSerializer(new StringRedisSerializer());
//                redisTemplate.boundValueOps(orderId).set(0);
                //每次重试进行加一操作
//                redisTemplate.boundValueOps(orderId).increment(1);
//                if ((Integer) redisTemplate.boundValueOps(orderId).get() == 3){
//                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
//                }
                //根据传来的广告图片类的订单号查看订单状态
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("order_id",orderId);

                List<TContentOrder> tContentOrderList = tContentOrderMapper.selectList(queryWrapper);
                if (tContentOrderList.size() > 0 && tContentOrderList.get(0) != null){    //为了防止下标越界先判断长度
                    Integer status = tContentOrderList.get(0).getStatus();
                    //如果状态为1就是已支付
                    if (status == 1){
                        //增加广告表
                        itContentService.insertContent(tContent1);
                        return  ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                    }else {//否则就将这个订单删除
                        tContentOrderMapper.delete(queryWrapper);
                        return  ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                    }
                }
                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
        });
    }
}
